import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import UnoCSS from "unocss/vite";
import { fileURLToPath, URL } from "node:url";
import viteCompression from "vite-plugin-compression";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";

const devServerUrl1 = "http://172.17.31.79";

// https://vite.dev/config/
export default defineConfig({
    base: "./",
    plugins: [
        vue(),
        UnoCSS(),
        AutoImport({
            resolvers: [ElementPlusResolver()],
            dts: "src/types/auto-imports.d.ts",
        }),
        Components({
            resolvers: [
                ElementPlusResolver({
                    importStyle: "css",
                }),
            ],
            dts: "src/types/components.d.ts",
        }),
        viteCompression({
            verbose: true,
            disable: false,
            threshold: 10240,
            algorithm: "gzip",
            ext: ".gz",
            deleteOriginFile: false,
        }),
    ],
    resolve: {
        alias: {
            "@": fileURLToPath(new URL("./src", import.meta.url)),
        },
        extensions: [".ts", ".tsx", ".js", ".jsx", ".vue", ".json"],
    },
    css: {
        preprocessorOptions: {
            scss: {
                additionalData: `@use "sass:color";`,
            },
        },
    },
    server: {
        port: 3008,
        open: true,
        cors: true,
        host: "0.0.0.0",
        hmr: {
            overlay: true,
        },
        proxy: {
            "/api/base": {
                target: devServerUrl1,
                changeOrigin: true,
                ws: true,
            },
        },
    },
    build: {
        outDir: "dist",
        assetsDir: "static",
        sourcemap: false,
        chunkSizeWarningLimit: 1500,
        rollupOptions: {
            output: {
                manualChunks: {
                    vue: ["vue", "vue-router"],
                    "element-plus": ["element-plus"],
                },
                chunkFileNames: "static/js/[name]-[hash].js",
                entryFileNames: "static/js/[name]-[hash].js",
                assetFileNames: "static/[ext]/[name]-[hash].[ext]",
            },
        },
        minify: "esbuild",
    },
    define: {
        __VUE_OPTIONS_API__: JSON.stringify(true),
        __VUE_PROD_DEVTOOLS__: JSON.stringify(false),
        __VUE_PROD_HYDRATION_MISMATCH_DETAILS__: JSON.stringify(false),
    },
    optimizeDeps: {
        include: ["vue", "vue-router", "element-plus"],
    },
});
